package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.CommunityParty;
import com.mohe.dangjian.model.Member;
import com.mohe.dangjian.model.PartyActivity;
import com.mohe.dangjian.service.interfaces.IPartyActivity;
import com.mohe.dangjian.utils.Page;

public class PartyActivityImpl implements IPartyActivity {

	private IDao<PartyActivity> partyActivityDao;
	
	public IDao<PartyActivity> getPartyActivityDao() {
		return partyActivityDao;
	}

	public void setPartyActivityDao(IDao<PartyActivity> partyActivityDao) {
		this.partyActivityDao = partyActivityDao;
	}

	@Override
	public List<PartyActivity> getPartyActivityList(
			PartyActivity partyActivity, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from PartyActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(partyActivity.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + partyActivity.getPartyName() + "%%");
		}
		// 活动名称
		if (!StringUtils.isBlank(partyActivity.getActivityName())) {
			where.append(" and activityName like ?");
			params.add("%%" + partyActivity.getActivityName() + "%%");
		}
		// 活动类型
		if (!StringUtils.isBlank(partyActivity.getActivityType())) {
			where.append(" and activityType like ?");
			params.add("%%" + partyActivity.getActivityType() + "%%");
		}
		// 日期
		if (!StringUtils.isBlank(partyActivity.getActivityDate())) {
			where.append(" and activityDate like ?");
			params.add("%%" + partyActivity.getActivityDate() + "%%");
		}
		// 活动地点
		if (!StringUtils.isBlank(partyActivity.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + partyActivity.getAddress() + "%%");
		}
		// 参加人数
		if (partyActivity.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(partyActivity.getAttendNum());
		}
		// 活动内容
		if (!StringUtils.isBlank(partyActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + partyActivity.getContent() + "%%");
		}
		// 活动情况
		if (!StringUtils.isBlank(partyActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + partyActivity.getSituation() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(partyActivity.getPartyType())) {
			where.append(" and partyType = ?");
			params.add(partyActivity.getPartyType());
		}	

		where.append(" order by createDate desc");
		List<PartyActivity> list = partyActivityDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(PartyActivity partyActivity) {
		StringBuffer where = new StringBuffer();
		where.append("from PartyActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(partyActivity.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + partyActivity.getPartyName() + "%%");
		}
		// 活动名称
		if (!StringUtils.isBlank(partyActivity.getActivityName())) {
			where.append(" and activityName like ?");
			params.add("%%" + partyActivity.getActivityName() + "%%");
		}
		// 活动类型
		if (!StringUtils.isBlank(partyActivity.getActivityType())) {
			where.append(" and activityType like ?");
			params.add("%%" + partyActivity.getActivityType() + "%%");
		}
		// 日期
		if (!StringUtils.isBlank(partyActivity.getActivityDate())) {
			where.append(" and activityDate like ?");
			params.add("%%" + partyActivity.getActivityDate() + "%%");
		}
		// 活动地点
		if (!StringUtils.isBlank(partyActivity.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + partyActivity.getAddress() + "%%");
		}
		// 参加人数
		if (partyActivity.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(partyActivity.getAttendNum());
		}
		// 活动内容
		if (!StringUtils.isBlank(partyActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + partyActivity.getContent() + "%%");
		}
		// 活动情况
		if (!StringUtils.isBlank(partyActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + partyActivity.getSituation() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(partyActivity.getPartyType())) {
			where.append(" and partyType = ?");
			params.add(partyActivity.getPartyType());
		}		
		
		return partyActivityDao.getCount(where.toString(), params);
	}

	@Override
	public boolean savePartyActivity(PartyActivity partyActivity) {
		return partyActivityDao.insert(partyActivity);
	}

	@Override
	public boolean savePartyActivityList(List<PartyActivity> partyActivityList) {
		return partyActivityDao.insert(partyActivityList);
	}

	@Override
	public PartyActivity getPartyActivityById(Long timeKey) {
		PartyActivity partyActivity = partyActivityDao.getByKey(timeKey);
		if (partyActivity != null) {
			return partyActivity;
		}
		return null;
	}

	@Override
	public boolean updatePartyActivity(PartyActivity partyActivity) {
		return partyActivityDao.update(partyActivity);
	}

	@Override
	public boolean delPartyActivity(Long timeKey) {
		return partyActivityDao.delete(timeKey);
	}

	@Override
	public List<PartyActivity> getAllPartyActivity() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return partyActivityDao.query(where.toString(), null);
	}

	@Override
	public List<PartyActivity> getPartyActivityList(PartyActivity partyActivity) {
		StringBuffer where = new StringBuffer();
		where.append("from PartyActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(partyActivity.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + partyActivity.getPartyName() + "%%");
		}
		// 活动名称
		if (!StringUtils.isBlank(partyActivity.getActivityName())) {
			where.append(" and activityName like ?");
			params.add("%%" + partyActivity.getActivityName() + "%%");
		}
		// 活动类型
		if (!StringUtils.isBlank(partyActivity.getActivityType())) {
			where.append(" and activityType like ?");
			params.add("%%" + partyActivity.getActivityType() + "%%");
		}
		// 日期
		if (!StringUtils.isBlank(partyActivity.getActivityDate())) {
			where.append(" and activityDate like ?");
			params.add("%%" + partyActivity.getActivityDate() + "%%");
		}
		// 活动地点
		if (!StringUtils.isBlank(partyActivity.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + partyActivity.getAddress() + "%%");
		}
		// 参加人数
		if (partyActivity.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(partyActivity.getAttendNum());
		}
		// 活动内容
		if (!StringUtils.isBlank(partyActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + partyActivity.getContent() + "%%");
		}
		// 活动情况
		if (!StringUtils.isBlank(partyActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + partyActivity.getSituation() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(partyActivity.getPartyType())) {
			where.append(" and partyType = ?");
			params.add(partyActivity.getPartyType());
		}	
		
		where.append(" order by createDate desc");
		List<PartyActivity> list = partyActivityDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

}
